package org.jeecg.modules.chg.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.system.vo.JwidModel;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.config.mybatis.TenantContext;
import org.jeecg.modules.arc.service.IArcAccountService;
import org.jeecg.modules.arc.vo.ArcAccountCusVO;
import org.jeecg.modules.chg.entity.ChgFees;
import org.jeecg.modules.chg.entity.ChgPayments;
import org.jeecg.modules.chg.entity.ChgSubPayments;
import org.jeecg.modules.chg.service.IChgMpService;
import org.jeecg.modules.chg.service.IChgPaymentsService;
import org.jeecg.modules.chg.service.IChgStoredsService;
import org.jeecg.modules.chg.service.IChgSubPaymentsService;
import org.jeecg.modules.chg.vo.ChgPaymentsPage;
import org.jeecg.modules.pay.service.IBillReceiveService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

 /**
 * @Description: 缴费记录
 * @Author: jeecg-boot
 * @Date:   2022-02-21
 * @Version: V1.0
 */
@Api(tags="缴费记录")
@RestController
@RequestMapping("/chg/chgPayments")
@Slf4j
public class ChgPaymentsController {
	@Autowired
	private IChgPaymentsService chgPaymentsService;
	@Autowired
	private IChgSubPaymentsService chgSubPaymentsService;
	@Autowired
	private IArcAccountService arcAccountService;
	@Autowired
	private IChgStoredsService chgStoredsService;
	@Autowired
	private IChgMpService chgMpService;
	@Resource(name = "billReceiveAbcService")
	private IBillReceiveService billReceiveAbcService;
	@Autowired
	private ISysBaseAPI baseAPI;

	/**
	 * 分页列表查询
	 *
	 * @param chgPayments
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "缴费记录-分页列表查询")
	@ApiOperation(value="缴费记录-分页列表查询", notes="缴费记录-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(ChgPayments chgPayments,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ChgPayments> queryWrapper = QueryGenerator.initQueryWrapper(chgPayments, req.getParameterMap());
		queryWrapper.eq("del_flag",0);
		Page<ChgPayments> page = new Page<ChgPayments>(pageNo, pageSize);
		IPage<ChgPayments> pageList = chgPaymentsService.page(page, queryWrapper);
		//动态找出规则表SKU
		pageList.getRecords().forEach(item->{
			item.setPayFee(item.getPayFee().subtract(item.getZeroFee()));
		});
		return Result.OK(pageList);
	}
	/**
	 * 分页列表查询-预缴-取消
	 *
	 * @param chgPayments
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "预缴-取消-缴费记录-分页列表查询")
	@ApiOperation(value="预缴-取消-分页列表查询", notes="缴费记录-分页列表查询")
	@GetMapping(value = "/cancelList")
	public Result<?> queryPageCancelList(ChgPayments chgPayments,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		Page<ChgPayments> page = new Page<ChgPayments>(pageNo, pageSize);
		//没有收费
		IPage<ChgPayments> pageList = chgPaymentsService.cancelPageList(page, chgPayments);
		//实付-找零
		pageList.getRecords().forEach(item->{
			item.setPayFee(item.getPayFee().subtract(item.getZeroFee()));
		});
		return Result.OK(pageList);
	}
	/**
	 * 分页列表查询
	 *
	 * @param chgPayments
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "缴费记录-分页列表查询")
	@ApiOperation(value="缴费记录-分页列表查询", notes="缴费记录-分页列表查询")
	@GetMapping(value = "/payList")
	public Result<?> queryPayList(ChgPayments chgPayments,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="100") Integer pageSize,
								   HttpServletRequest req) {
		if(oConvertUtils.isNotEmpty(chgPayments.getAccountId())
				|| oConvertUtils.isNotEmpty(chgPayments.getAccCode())){

			List<ChgPayments> chgPayments1 = chgPaymentsService.selectBillNoOpen(chgPayments.getAccCode()
					, chgPayments.getCreateTimeBegin(), chgPayments.getCreateTimeEnd());

			IPage<ChgPayments> pageList = new Page<>(1, 100, chgPayments1.size());
			pageList.setRecords(chgPayments1);

			return Result.OK(pageList);
		}else{
			QueryWrapper<ChgPayments> queryWrapper = QueryGenerator.initQueryWrapper(chgPayments, req.getParameterMap());
			queryWrapper.eq("id","111111111111111");
			Page<ChgPayments> page = new Page<ChgPayments>(pageNo, pageSize);
			IPage<ChgPayments> pageList = chgPaymentsService.page(page, queryWrapper);
			return Result.OK(pageList);
		}
	}
	/**
	 * phone-缴费记录
	 *
	 * @param chgPayments
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "phone-缴费记录")
	@ApiOperation(value="phone-缴费记录", notes="phone-缴费记录")
	@GetMapping(value = "/getPayList")
	public Result<?> getPayList(ChgPayments chgPayments,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="100") Integer pageSize,
								   HttpServletRequest req) {
		// if(oConvertUtils.isEmpty(TenantContext.getTenant())){
			LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			String tenantIds = sysUser.getRelTenantIds();
			if (oConvertUtils.isNotEmpty(tenantIds)) {
				List<String> tenantIdList = Arrays.asList(tenantIds.split(","));
				TenantContext.setTenant(tenantIdList.get(0));
			}
		// }

		List<DictModel> dictItems = baseAPI.getDictItems("fee_pay_way");
		Map<String,String>wayDict=new HashMap<>();
		for (DictModel dictItem : dictItems) {
			wayDict.put(dictItem.getValue(),dictItem.getText());
		}


		if(oConvertUtils.isNotEmpty(chgPayments.getAccountId())
				|| oConvertUtils.isNotEmpty(chgPayments.getAccCode())){

			LambdaQueryWrapper<ChgPayments> paymentLqw = new LambdaQueryWrapper<>();
			paymentLqw.eq(ChgPayments::getAccCode, chgPayments.getAccCode());
			paymentLqw.eq(ChgPayments::getDelFlag, 0).eq(ChgPayments::getStatus, 1);
			paymentLqw.between(ChgPayments::getCreateTime, chgPayments.getCreateTimeBegin(), chgPayments.getCreateTimeEnd());
			if(oConvertUtils.isNotEmpty(chgPayments.getFeePayWay())){
				paymentLqw.eq(ChgPayments::getFeePayWay, chgPayments.getFeePayWay());
			}
			List<ChgPayments> paymentsList = chgPaymentsService.list(paymentLqw);

			JSONObject payObj=new JSONObject();
			JSONArray pdObj=new JSONArray();
			JSONArray feePayWay=new JSONArray();
			BigDecimal payMoney = new BigDecimal(0.0);
			BigDecimal kouMoney = new BigDecimal(0.0);
			String accCode="";
			String accName="";
			Integer payNumber=0;
			if(null != paymentsList && paymentsList.size()>0){
				List<String>payIds=new ArrayList<>();
				for (ChgPayments payments : paymentsList) {
					payMoney=payMoney.add(payments.getPayFee().subtract(payments.getZeroFee()));

					if(payments.getPayFee().doubleValue()>0){
						JSONObject jsonObject = new JSONObject();
						jsonObject.put("wayDate", DateUtils.formatDate(payments.getCreateTime(),"yyyyMMdd"));
						jsonObject.put("payWay",wayDict.get(payments.getFeePayWay()));
						jsonObject.put("wayMoney",payments.getPayFee().subtract(payments.getZeroFee()));
						feePayWay.add(jsonObject);
					}

					accCode=payments.getAccCode();
					accName=payments.getAccName();
					payIds.add(payments.getId());
				}
				payNumber=paymentsList.size();

				List<ChgFees> chgPayFeeDetail = chgStoredsService.getChgPayFeeDetail(accCode
						,chgPayments.getCreateTimeBegin(), chgPayments.getCreateTimeEnd());
				if(null != chgPayFeeDetail && chgPayFeeDetail.size()>0){
					for (ChgFees chgFees : chgPayFeeDetail) {
						kouMoney=kouMoney.add(chgFees.getMoney());

						JSONObject fee =new JSONObject();
						fee.put("cusCode",chgFees.getCusId());
						fee.put("cusName",chgFees.getCusName());
						fee.put("readMonth",chgFees.getReadMonth());
						fee.put("beginScale",chgFees.getBeginScale());
						fee.put("endScale",chgFees.getEndScale());
						fee.put("amount",chgFees.getAmount());
						fee.put("totalMoney",chgFees.getTotalMoney());
						fee.put("wuMoney",chgFees.getWuFee());
						fee.put("ljMoney",chgFees.getLjMoney());
						fee.put("penalty",chgFees.getPenalty());
						fee.put("preBalance",chgFees.getPreBalance());
						fee.put("postBalance",chgFees.getPostBalance());
						fee.put("money",chgFees.getMoney().subtract(chgFees.getRecivedMoney()));
						fee.put("datetime",chgFees.getFinancialDate());
						pdObj.add(fee);
					}
				}
			}else{
				List<ArcAccountCusVO> arcAccountCusVOS = arcAccountService.queryAccount(chgPayments.getAccountId()
						, null, chgPayments.getAccCode());
				if(null !=arcAccountCusVOS && arcAccountCusVOS.size()>0){
					ArcAccountCusVO accountCusVO = arcAccountCusVOS.get(0);
					accCode=accountCusVO.getAccCode();
					accName=accountCusVO.getAccName();
				}
			}
			payObj.put("accCode",accCode);
			payObj.put("accName",accName);
			payObj.put("payNumber",payNumber);
			payObj.put("payMoney",payMoney);
			payObj.put("kouMoney",kouMoney);
			payObj.put("feePayWay",feePayWay);
			payObj.put("changeMoney",payMoney.subtract(kouMoney).setScale(2, BigDecimal.ROUND_HALF_UP));
			payObj.put("payDetail",pdObj);
			return Result.OK(payObj);
		}else{
			JSONObject payObj=new JSONObject();
			BigDecimal payMoney = new BigDecimal(0.0);
			String accCode="";
			String accName="";
			Integer payNumber=0;

			payObj.put("accCode",accCode);
			payObj.put("accName",accName);
			payObj.put("payNumber",payNumber);
			payObj.put("payMoney",payMoney);
			return Result.OK(payObj);
		}
	}

	/**
	 *   添加
	 *
	 * @param chgPaymentsPage
	 * @return
	 */
	@AutoLog(value = "缴费记录-添加")
	@ApiOperation(value="缴费记录-添加", notes="缴费记录-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody ChgPaymentsPage chgPaymentsPage) {
		ChgPayments chgPayments = new ChgPayments();
		BeanUtils.copyProperties(chgPaymentsPage, chgPayments);
		chgPaymentsService.saveMain(chgPayments, chgPaymentsPage.getChgSubPaymentsList());
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param chgPaymentsPage
	 * @return
	 */
	@AutoLog(value = "缴费记录-编辑")
	@ApiOperation(value="缴费记录-编辑", notes="缴费记录-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody ChgPaymentsPage chgPaymentsPage) {
		ChgPayments chgPayments = new ChgPayments();
		BeanUtils.copyProperties(chgPaymentsPage, chgPayments);
		ChgPayments chgPaymentsEntity = chgPaymentsService.getById(chgPayments.getId());
		if(chgPaymentsEntity==null) {
			return Result.error("未找到对应数据");
		}
		chgPaymentsService.updateMain(chgPayments, chgPaymentsPage.getChgSubPaymentsList());
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "缴费记录-通过id删除")
	@ApiOperation(value="缴费记录-通过id删除", notes="缴费记录-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		ChgPayments chgPayments = chgPaymentsService.getById(id);
		if(chgPayments.getStatus()>0){
			return Result.error("已产生业务不能删除!");
		}
		chgPaymentsService.delMain(id);
		return Result.OK("删除成功!");
	}

//	/**
//	 *  批量删除
//	 *
//	 * @param ids
//	 * @return
//	 */
//	@AutoLog(value = "缴费记录-批量删除")
//	@ApiOperation(value="缴费记录-批量删除", notes="缴费记录-批量删除")
//	@DeleteMapping(value = "/deleteBatch")
//	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
//		this.chgPaymentsService.delBatchMain(Arrays.asList(ids.split(",")));
//		return Result.OK("批量删除成功！");
//	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "缴费记录-通过id查询")
	@ApiOperation(value="缴费记录-通过id查询", notes="缴费记录-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		ChgPayments chgPayments = chgPaymentsService.getById(id);
		if(chgPayments==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(chgPayments);

	}

	 /**
	  *   取消缴费取消收费-
	  *
	  * @param chgPayments
	  * @return
	  */
	 @AutoLog(value = "取消缴费取消收费")
	 @ApiOperation(value="取消缴费取消收费", notes="取消缴费取消收费")
	 @PostMapping(value = "/cancelPay")
	 public Result<?> cancelPay(@RequestBody ChgPayments chgPayments) {
		 return chgPaymentsService.cancelPay(chgPayments);
	 }

	 /**
	  *   取消农行收费
	  *
	  * @param chgPayments
	  * @return
	  */
	 @AutoLog(value = "取消农行收费")
	 @ApiOperation(value="取消农行收费", notes="取消农行收费")
	 @PostMapping(value = "/cancelAbcPay")
	 public Result<?> cancelAbcPay(@RequestBody ChgPayments chgPayments) {
		 ChgPayments payments = chgPaymentsService.getById(chgPayments.getId());
		 try {
			 if(payments.getStatus()==0){
				 return Result.OK("已取消成功");
			 }
			 if(!"5".equals(payments.getFeePayWay())){
				 return Result.error("不是农行支付");
			 }
			 JSONObject jsonObject = billReceiveAbcService.cancelAbcPay(payments);
			 if("200".equals(jsonObject.getString("code"))){
				 //payments.setStatus(2);
				 //chgPaymentsService.updateById(payments);
			 	return Result.OK("取消成功");
			 }else{
				 return Result.error(jsonObject.getString("msg"));
			 }
		 } catch (Exception e) {
			 e.printStackTrace();
		 }
		 return Result.error("取消异常");
	 }


	 /**
	  * 手动核销微信订单
	  *
	  * @return
	  */
	 @AutoLog(value = "缴费记录-通过id查询")
	 @ApiOperation(value="缴费记录-通过id查询", notes="缴费记录-通过id查询")
	 @PostMapping(value = "/queryOrder")
	 public Result<?> queryOrder(@RequestBody ChgPaymentsPage chgPaymentsPage) {
		 ChgPayments chgPayments = chgPaymentsService.getById(chgPaymentsPage.getId());
		 if(chgPayments==null) {
			 return Result.error("未找到对应数据");
		 }
		 if(1 == chgPayments.getStatus() && oConvertUtils.isNotEmpty(chgPayments.getTransactionId())) {
			 return Result.error("已是成功支付状态");
		 }
		 if("5".equals(chgPayments.getFeePayWay())){
			 JSONObject jsonObject = billReceiveAbcService.abcBillQuery(chgPayments);
			 if("200".equals(jsonObject.getString("code"))){
				 return Result.OK(jsonObject.getString("msg"));
			 }else{
				 return Result.error(jsonObject.getString("msg"));
			 }
		 }else if("3".equals(chgPayments.getFeePayWay())){

			 if(1 == chgPayments.getStatus() && chgPayments.getSyncFlag() ==1) {
				 return Result.error("已核销");
			 }

			 if(oConvertUtils.isEmpty(chgPaymentsPage.getArrearsIds()) || "[]".equals(chgPaymentsPage.getArrearsIds())){
				//没有选择欠费核销，即为预缴,用 ClientId 暂存
				 chgPayments.setClientId(null);
			 }else{
				 chgPayments.setClientId(chgPaymentsPage.getArrearsIds());
			 }

			 JSONObject object = new JSONObject();
			 object.put("tenantId",chgPayments.getTenantId());
			 JwidModel jwidModel = baseAPI.getJwId(object);

			 return chgMpService.queryOrderState(jwidModel,chgPayments);
		 }
		 return Result.error("不是核销类型");
	 }

	 /**
	  * 根据微信交易单号查询订单状态
	  *
	  * @return
	  */
	 @AutoLog(value = "微信交易单号查询订单状态")
	 @ApiOperation(value="微信交易单号查询订单状态", notes="微信交易单号查询订单状态")
	 @PostMapping(value = "/queryWxtOrder")
	 public Result<?> queryWxtOrder(@RequestBody ChgPaymentsPage chgPaymentsPage) {
		 ChgPayments chgPayments =new ChgPayments();// chgPaymentsService.getById(chgPaymentsPage.getId());
//		 if(chgPayments==null) {
//			 return Result.error("未找到对应数据");
//		 }
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 if(oConvertUtils.isEmpty(chgPaymentsPage.getTransactionId())) {
			 return Result.error("交易单号不能为空");
		 }
		 chgPayments.setTransactionId(chgPaymentsPage.getTransactionId());

		 JSONObject object = new JSONObject();
		 object.put("tenantId",sysUser.getRelTenantIds());
		 JwidModel jwidModel = baseAPI.getJwId(object);

		 return chgMpService.queryWxtOrderState(jwidModel,chgPayments);

	 }

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "缴费明细-通过主表ID查询")
	@ApiOperation(value="缴费明细-通过主表ID查询", notes="缴费明细-通过主表ID查询")
	@GetMapping(value = "/queryChgSubPaymentsByMainId")
	public Result<?> queryChgSubPaymentsListByMainId(@RequestParam(name="id",required=true) String id) {
		List<ChgSubPayments> chgSubPaymentsList = chgSubPaymentsService.selectByMainId(id);
		IPage <ChgSubPayments> page = new Page<>();
		page.setRecords(chgSubPaymentsList);
		page.setTotal(chgSubPaymentsList.size());
		return Result.OK(page);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param chgPayments
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ChgPayments chgPayments) {
      // Step.1 组装查询条件查询数据
      QueryWrapper<ChgPayments> queryWrapper = QueryGenerator.initQueryWrapper(chgPayments, request.getParameterMap());
      LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

      //Step.2 获取导出数据
      List<ChgPayments> queryList = chgPaymentsService.list(queryWrapper);
      // 过滤选中数据
      String selections = request.getParameter("selections");
      List<ChgPayments> chgPaymentsList = new ArrayList<ChgPayments>();
      if(oConvertUtils.isEmpty(selections)) {
          chgPaymentsList = queryList;
      }else {
          List<String> selectionList = Arrays.asList(selections.split(","));
          chgPaymentsList = queryList.stream().filter(item -> selectionList.contains(item.getId())).collect(Collectors.toList());
      }

      // Step.3 组装pageList
      List<ChgPaymentsPage> pageList = new ArrayList<ChgPaymentsPage>();
      for (ChgPayments main : chgPaymentsList) {
          ChgPaymentsPage vo = new ChgPaymentsPage();
          BeanUtils.copyProperties(main, vo);
          List<ChgSubPayments> chgSubPaymentsList = chgSubPaymentsService.selectByMainId(main.getId());
          vo.setChgSubPaymentsList(chgSubPaymentsList);
          pageList.add(vo);
      }

      // Step.4 AutoPoi 导出Excel
      ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
      mv.addObject(NormalExcelConstants.FILE_NAME, "缴费记录列表");
      mv.addObject(NormalExcelConstants.CLASS, ChgPaymentsPage.class);
      mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("缴费记录数据", "导出人:"+sysUser.getRealname(), "缴费记录"));
      mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
      return mv;
    }

    /**
    * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
      Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
      for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
          MultipartFile file = entity.getValue();// 获取上传文件对象
          ImportParams params = new ImportParams();
          params.setTitleRows(2);
          params.setHeadRows(1);
          params.setNeedSave(true);
          try {
              List<ChgPaymentsPage> list = ExcelImportUtil.importExcel(file.getInputStream(), ChgPaymentsPage.class, params);
              for (ChgPaymentsPage page : list) {
                  ChgPayments po = new ChgPayments();
                  BeanUtils.copyProperties(page, po);
                  chgPaymentsService.saveMain(po, page.getChgSubPaymentsList());
              }
              return Result.OK("文件导入成功！数据行数:" + list.size());
          } catch (Exception e) {
              log.error(e.getMessage(),e);
              return Result.error("文件导入失败:"+e.getMessage());
          } finally {
              try {
                  file.getInputStream().close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
      return Result.OK("文件导入失败！");
    }

}
